/*
 * Copyright (C) 2018 The Asiainfo-Ability Authors
 *
 *      Licensed under the Apache License, Version 2.0 (the "License");
 *      you may not use this file except in compliance with the License.
 *      You may obtain a copy of the License at
 *
 *          http://www.apache.org/licenses/LICENSE-2.0
 *
 *      Unless required by applicable law or agreed to in writing, software
 *      distributed under the License is distributed on an "AS IS" BASIS,
 *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *      See the License for the specific language governing permissions and
 *      limitations under the License.
 */

package org.asiainfo.ability.manager.controler;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.asiainfo.ability.base.utils.*;
import org.asiainfo.ability.gateway.cache.CacheService;
import org.asiainfo.ability.base.code.Constants;
import org.asiainfo.ability.gateway.code.OnOffState;
import org.asiainfo.ability.gateway.dao.AbilityDao;
import org.asiainfo.ability.gateway.dao.ClientDao;
import org.asiainfo.ability.gateway.model.Ability;
import org.asiainfo.ability.gateway.vo.Token;
import org.asiainfo.ability.gateway.vo.TokenResp;
import org.asiainfo.ability.manager.code.SessionKey;
import org.asiainfo.ability.manager.dao.StaffDao;
import org.asiainfo.ability.manager.model.Staff;
import org.asiainfo.ability.manager.service.AdminService;
import org.asiainfo.ability.manager.utils.SHAxxx;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author: visen
 * @Date: 2018/03/02
 * @Description:
 */
@RestController
public class StaffControler {
    public static final Logger logger = LogManager.getLogger(StaffControler.class);


    private Sequence seqUtils;
    @PostConstruct
    public void init() {
        seqUtils = SeqUtils.newInstance();
    }

    @Autowired
    private StaffDao staffDao;




    @RequestMapping(value = "/admin/login", method = RequestMethod.POST)
    public @ResponseBody
    String login(HttpServletRequest request, HttpServletResponse response) {

        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        if(StringUtils.isBlank(userName) || StringUtils.isBlank(password)) {
            return Constants.FAIL;
        }
        Staff staff = staffDao.getUserbyUserName(userName);
        if(staff.getState() == OnOffState.off) {
            return Constants.FAIL;
        }
        if(!staff.getPassword().equals(SHAxxx.md5(password))) {
            return Constants.FAIL;
        }
        putSession(request, SessionKey.STAFF, staff);
        return Constants.OK;

    }

    public void putSession(HttpServletRequest request, String key, Object value) {
        HttpSession session = request.getSession(true);
        session.setAttribute(key, value);
    }
    @RequestMapping(value = "/admin/staff", method = RequestMethod.POST)
    public @ResponseBody
    String createStaff(@RequestBody Staff staff, HttpServletResponse response) {
        staff.setId(String.valueOf(seqUtils.id()));
        staff.setUpdateTime(new Date());
        staffDao.saveStaff(staff);
        return "1";

    }
    public static void main(String[] args) {

    }
}
